package cn.edu.sdjzu.xg.kcsj.controller.basic;

import cn.edu.sdjzu.xg.kcsj.domain.Teacher;
import cn.edu.sdjzu.xg.kcsj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/updateTeacher.ctl")
public class UpdateTeacherController extends HttpServlet {
    @Override
    protected void doPut (HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String teacher_json = JSONUtil.getJSON(req);
        Teacher teacherToAdd = JSON.parseObject(teacher_json,Teacher.class);
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            if (TeacherService.getInstance().update(teacherToAdd)){
                message.put("message", "update yes");
            }else{
                message.put ("meseage","update no");
            }
        } catch (SQLException e) {
            message.put("message", "sjk have exception");
        } catch (Exception e) {
            message.put("message", "wl have exception");
        }
        resp.getWriter().println(message);
    }
}
